/**
 * @file geminai_frig.h
 * @brief 制冷逻辑
 * @author zl
 */

#ifndef __GEMINAI_FRIG__
#define __GEMINAI_FRIG__

#include <stdio.h>
#include <string.h>
#include "geminai_mqtt_cache.h"
#include "cJSON.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "geminai_gpio.h"
#include "geminai_para.h"
#include "geminai_CH455.h"
#include "geminai_led_pwm.h"
#include "geminai_selfcheck.h"
#include <freertos/timers.h>

//数码管显示
#define ALARM_OVERTEMP		0X01	//高温报警
#define ALARM_LOWTEMP			0X02	//低温报警
#define ALARM_DOOR				0x04	//开门超时报警
#define ALARM_DEFORST			0x08	//除霜
#define ALARM_FORCED_COOL	0X10	//强制制冷
#define ALARM_NTC1_ERROR		0x20	//柜内温度传感器异常
#define ALARM_NTC2_ERROR	0x40//环境传感器异常
#define ALARM_IS_COMPRESSOR_DELAY 0X80  //处于最小停机时间
#define ALARM_OPEN_LED			0X100	//开灯
#define ALARM_CLOSE_LED			0x200	//关灯

#define TEMP_ERR_UPPER		(60)//温度传感器故障值上限
#define TEMP_ERR_LOWER		(-15)//温度传感器故障值下限

typedef enum {RESET = 0, SET = !RESET} FlagStatus;

typedef enum{
	FRIG_STAT_IDLE = 0,
	FRIG_STAT_ERR_MODE = 1,
	FRIG_STAT_NORMAL_MODE = 2,
	FRIG_STAT_DEFROST_MODE = 3,
	FRIG_STAT_FORCE_MODE = 4,
}frig_ctl_stat;

typedef struct{
	uint8_t state;
	uint32_t timer;
	uint8_t err_mode_toggle;
}frig_ctr;

//主动上报事件列表
typedef enum{
	EVENT_LED_SW=0,				//灯光开关
	EVENT_FRIG_SW,				//制冷开关
    EVENT_FRIG_NORMAL_STATE,	//正常状态
	EVENT_FRIG_DEFROST_STATE,	//除霜状态
    EVENT_FRIG_FORCED_STATE,	//强制制冷状态
	EVENT_FRIG_RUN_MODE,			//运行模式
	EVENT_OVERTEMP_ALARM,		//高温报警
	EVENT_LOWTEMP_ALARM,		//低温报警
}event_stat;

typedef struct{
	uint32_t comp_wk_tim_total;//压缩机累计运行时间
	uint32_t comp_wk_tim_cont;//压缩机连续运行时间
	uint32_t ctrl_up_time;//控制器上电时间
	uint32_t defrost_trust_tim;//除霜判定时间(压机累计运行或连续运行时间)
	uint32_t defrost_tim;//除霜时间
	uint16_t defrost_work_time;//除霜累计工作时长	
}defrost_ctr;

// @brief 风机状态置为工作状态(开启)
void fans_set_work(void);
// @brief 风机状态置为不工作状态(关闭)
void fans_reset_work(void);

void frig_event_set(event_stat event);
void frig_task(void);
void frig_set_stat(uint8_t _state, uint32_t tim);
int aiot_get_frig_stat(void);
uint16_t dafrost_exec_get(void);

#endif